import { Injectable } from '@nestjs/common';
import { getCurrentTime, beanCopyArray, formatTime } from 'src/utils';
import { Response } from 'src/utils/response';
import { BacklogVO } from './backlog.vo';
import { BacklogRepository } from './backlog.repository';
import { BacklogAddDto } from './dto/add.dto';

@Injectable()
export class BacklogService {
  constructor(private readonly backlogRepository: BacklogRepository) {}

  async queryBacklogService(DTO: any) {
    if (!DTO.createdBy) {
      return Response.error({
        message: '未传递用户id',
      });
    }
    const dataList =
      await this.backlogRepository.queryBacklogListRepository(DTO);

    const VO = new BacklogVO();
    const result: BacklogVO[] = beanCopyArray<BacklogVO>(VO, dataList);
    result.forEach((backlog) => {
      formatTime(backlog, ['stopAt'], 'YYYY-MM-DD');
    });
    return Response.success({
      data: result,
    });
  }

  async addBacklogService(dto: BacklogAddDto) {
    if (!dto.createdBy) {
      return Response.error({
        message: '未传递用户id',
      });
    }
    const dataList =
      await this.backlogRepository.queryALlBacklogListRepository();
    const newDot = {
      ...dto,
      delFlag: 0,
      isEnd: 0,
      index: dataList.length + 1,
    };
    // @ts-ignore
    const backlog = await this.backlogRepository.addBacklogRepository(newDot);
    if (backlog) {
      return Response.success({
        message: '新增成功',
      });
    } else {
      return Response.error({
        message: '新增失败',
        data: backlog,
      });
    }
  }

  async accomplishService(params: { createdBy: number }) {
    if (!params.createdBy) {
      return Response.error({ message: '未传递用户id' });
    }
    const dataList =
      await this.backlogRepository.queryBacklogListRepository(params);
    const unfinishedList = dataList.filter(
      (backlog) => Number(backlog.isEnd) === 0,
    );
    const completeList = dataList.filter(
      (backlog) => Number(backlog.isEnd) === 1,
    );
    return Response.success({
      data: {
        unfinished: unfinishedList.length,
        complete: completeList.length,
      },
    });
  }

  // 删除
  async deleteBacklogService(id: number) {
    if (!id) {
      return Response.error({
        message: 'id is not exist',
      });
    }
    const result = await this.backlogRepository.deleteBacklogRepository(id);
    if (result.affected === 0) {
      return Response.error({
        message: 'delete fail',
      });
    }
    return Response.success({
      message: 'delete success',
    });
  }

  // 完成待办
  async completeBacklogService(id: number) {
    if (!id) {
      return Response.error({
        message: 'id is not exist',
      });
    }
    const DTO: any = {
      isEnd: 1,
      updatedAt: getCurrentTime('YYYY-MM-DD HH:mm:ss'),
    };
    const result = await this.backlogRepository.updateBacklogRepository(
      id,
      DTO,
    );
    if (result.affected === 0) {
      return Response.error({
        message: 'update fail',
      });
    }
    return Response.success({
      message: 'update success',
    });
  }

  // 更新
  async updateBacklogService(id: number, DTO: any) {
    if (!id) {
      return Response.error({
        message: 'id is not exist',
      });
    }
    DTO = {
      ...DTO,
      updatedAt: getCurrentTime('YYYY-MM-DD HH:mm:ss'),
    };
    const result = await this.backlogRepository.updateBacklogRepository(
      id,
      DTO,
    );
    if (result.affected === 0) {
      return Response.error({
        message: 'update fail',
      });
    }
    return Response.success({
      message: 'update success',
    });
  }
}
